package com.yanqu.road.server.manager.activity.hanghai;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hanghai.HangHaiLockStatus;
import com.yanqu.road.entity.activity.hanghai.HangHaiPort;
import com.yanqu.road.entity.activity.hanghai.HangHaiUnionData;
import com.yanqu.road.entity.activity.hanghai.HangHaiUserData;
import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.bussiness.activity.HangHaiActivityBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHangHaiActivityGroup {

    private final Object lock = new Object();

    // 活动
    private Map<Integer, CrossHangHaiActivity> groupMap = new ConcurrentHashMap<>();

    // 配置
    private ActivityInfo activityInfo;
    private HangHaiConfig hangHaiConfig;

    // 数据
    private Map<Long, HangHaiUserData> userDataMap = new ConcurrentHashMap<>();
    private Map<String, HangHaiUnionData> unionDataMap = new ConcurrentHashMap<>();
    private HangHaiLockStatus hangHaiLockStatus;


    public CrossHangHaiActivityGroup(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
        loadDbConfig();
        loadDbData();
    }

    public void loadDbConfig() {
        int activityId = activityInfo.getActivityId();

        // 加载配置
        HangHaiConfig tmpConfig = new HangHaiConfig();
        tmpConfig.setActivityId(activityId);
        tmpConfig.setActivityInfo(activityInfo);

        // 货物
        tmpConfig.setGoodsConfigMap(HangHaiActivityBusiness.getHangHaiGoodsConfigMap(activityId));
        // 港口
        tmpConfig.setPortConfigMap(HangHaiActivityBusiness.getHangHaiPortConfigMap(activityId));
        // 船
        tmpConfig.setShipConfigList(HangHaiActivityBusiness.getHangHaiShipConfigMap(activityId));

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(Collections.singletonList(activityId)).get(activityId);

        List<Integer> operationTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_OPERATION_TIME_PARAM.getName()).getIntListValue(";");
        tmpConfig.setStartOperationTime(operationTimeParam.get(0) * 60L * 1000L);
        tmpConfig.setEndOperationTime(operationTimeParam.get(1) * 60L * 1000L);

        tmpConfig.setBaseMoney(activityConfigMap.get(eActivityConfigType.HANGHAI_BASE_MONEY.getName()).getIntValue());

        tmpConfig.setTitleMoneyAddition(activityConfigMap.get(eActivityConfigType.HANGHAI_TITLE_MONEY_ADDITION.getName()).getIntValue());

        List<Integer> refreshTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REFRESH_TIME_LIST.getName()).getIntListValue(";");
        List<Long> refreshTimeList = new ArrayList<>();
        for (Integer refreshTime : refreshTimeParam) {
            refreshTimeList.add(refreshTime * 60L * 1000L);
        }
        tmpConfig.setRefreshTimeList(refreshTimeList);

        List<Integer> refreshStockTimeParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REFRESH_STOCK_TIME_LIST.getName()).getIntListValue(";");
        List<Long> refreshStockTimeList = new ArrayList<>();
        for (Integer refreshTime : refreshStockTimeParam) {
            refreshStockTimeList.add(refreshTime * 60L * 1000L);
        }
        tmpConfig.setRefreshStockTimeList(refreshStockTimeList);

        tmpConfig.setMoveTime(activityConfigMap.get(eActivityConfigType.HANGHAI_MOVE_TIME.getName()).getLongValue() * 60L * 1000L);

        tmpConfig.setSpecStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEC_STOCK_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setCommonStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_COMMON_STOCK_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setLackStockParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_LACK_COUNT_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setSpecPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEC_PRICE_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setCommonPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_COMMON_PRICE_PARAM.getName()).getIntListValue(";"));
        tmpConfig.setLackPriceParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_LACK_PRICE_PARAM.getName()).getIntListValue(";"));

        tmpConfig.setSpeedUpItemId(activityConfigMap.get(eActivityConfigType.HANGHAI_SPEED_UP_ITEM_ID.getName()).getIntValue());

        tmpConfig.setMoneyItemId(activityConfigMap.get(eActivityConfigType.HANGHAI_MONEY_ITEM_ID.getName()).getIntValue());

        String reportRewardParam = activityConfigMap.get(eActivityConfigType.HANGHAI_REPORT_REWARD_PARAM.getName()).getValue();
        for (String param : StringUtils.stringToStringList(reportRewardParam, "\\|")) {
            List<String> paramList = StringUtils.stringToStringList(param, ",");
            tmpConfig.getReportTimesList().add(Integer.valueOf(paramList.get(0)));
            tmpConfig.getReportRewardList().add(paramList.get(1));
        }

        tmpConfig.setUnionMinMoneyParamList(activityConfigMap.get(eActivityConfigType.HANGHAI_UNION_MIN_MONEY_PARAM.getName()).getIntListValue(";"));

        hangHaiConfig = tmpConfig;
    }

    public boolean loadDbData() {
        hangHaiLockStatus = HangHaiActivityBusiness.getHangHaiLockStatus(activityInfo.getActivityId());
        if (hangHaiLockStatus == null) {
            hangHaiLockStatus = new HangHaiLockStatus();
            hangHaiLockStatus.setActivityId(activityInfo.getActivityId());
            hangHaiLockStatus.setHadLock(false);
            hangHaiLockStatus.setInsertOption();
        }
        userDataMap.putAll(HangHaiActivityBusiness.getHangHaiUserDataMap(activityInfo.getActivityId()));
        unionDataMap.putAll(HangHaiActivityBusiness.getHangHaiUnionDataMap(activityInfo.getActivityId()));

        Map<String, Map<Integer, HangHaiPort>> hangHaiPortMap = HangHaiActivityBusiness.getHangHaiPortMap(activityInfo.getActivityId());
        for (HangHaiUnionData unionData : unionDataMap.values()) {
            Map<Integer, HangHaiPort> portMap = hangHaiPortMap.get(unionData.getUnionUid());
            unionData.setPortMap(portMap);
        }
        return true;
    }

    public boolean save() {
        // 商会数据
        for (HangHaiUnionData unionData : unionDataMap.values()) {
            if (unionData.isInsertOption()) {
                HangHaiActivityBusiness.addHangHaiUnionData(unionData);
            } else if (unionData.isUpdateOption()) {
                HangHaiActivityBusiness.updateHangHaiUnionData(unionData);
            }
            // 港口数据
            for (HangHaiPort hangHaiPort : unionData.getPortMap().values()) {
                if (hangHaiPort.isInsertOption()) {
                    HangHaiActivityBusiness.addHangHaiPort(hangHaiPort);
                } else if (hangHaiPort.isUpdateOption()) {
                    HangHaiActivityBusiness.updateHangHaiPort(hangHaiPort);
                }
            }
        }
        // 玩家数据
        for (HangHaiUserData hangHaiUserData : userDataMap.values()) {
            if (hangHaiUserData.getPortId() == 0) {
                synchronized (lock) {
                    if (hangHaiUserData.getPortId() == 0) {
                        hangHaiUserData.setPortId(CrossHangHaiActivity.getRandomPortId(hangHaiConfig));
                    }
                }
            }
            if (hangHaiUserData.isInsertOption()) {
                HangHaiActivityBusiness.addHangHaiUserData(hangHaiUserData);
            } else if (hangHaiUserData.isUpdateOption()) {
                HangHaiActivityBusiness.updateHangHaiUserData(hangHaiUserData);
            }
        }
        // 锁定数据
        if (hangHaiLockStatus.isInsertOption()) {
            HangHaiActivityBusiness.addHangHaiLockStatus(hangHaiLockStatus);
        } else if (hangHaiLockStatus.isUpdateOption()) {
            HangHaiActivityBusiness.updateHangHaiLockStatus(hangHaiLockStatus);
        }
        return true;
    }

    public CrossHangHaiActivity getActivity(int groupId) {

        if (!groupMap.containsKey(groupId)) {
            synchronized (lock) {
                if (!groupMap.containsKey(groupId)) {
                    CrossHangHaiActivity activity = new CrossHangHaiActivity(this, groupId, hangHaiConfig);
                    groupMap.put(groupId, activity);
                }
            }
        }

        return groupMap.get(groupId);
    }

    public Set<Long> getMemberList(String unionUid) {
        UnionActivityGroupUnion union = Cross2UnionActivityGroupMgr.getUnionData(activityInfo.getActivityId(), unionUid);
        if (union != null) {
            return union.getUserMap().keySet();
        }
        return new HashSet<>();
    }

    // -----------------------------------------------


    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public Map<Integer, CrossHangHaiActivity> getGroupMap() {
        return groupMap;
    }

    public Map<Long, HangHaiUserData> getUserDataMap() {
        return userDataMap;
    }

    public Map<String, HangHaiUnionData> getUnionDataMap() {
        return unionDataMap;
    }

    public HangHaiLockStatus getHangHaiLockStatus() {
        return hangHaiLockStatus;
    }

    public HangHaiConfig getHangHaiConfig() {
        return hangHaiConfig;
    }
}
